//
// Created by 123 on 2024/3/2.
//
#include <iostream>

using namespace std;

typedef struct LinkNode{
    int data;
    LinkNode *next;

}LinkNode,*LinkStack;

//初始化
bool initStack(LinkStack &l);
//判空栈
bool isEmpty(LinkStack l);
//进栈
bool push(LinkStack &l,int e);
//出栈
bool pop(LinkStack &l,int &e);
//读取栈顶元素
bool getTop(LinkStack &l,int &e);

void show(LinkStack l);


int main(){
    LinkStack l;
    initStack(l);
    show(l);
    cout<<isEmpty(l)<<"  <========"<<endl;
    cout<<"--------------------------"<<endl;
    int e=-1;
    cout<<pop(l,e)<<endl;
    cout<<e<<"   <--------------"<<endl;
    push(l,1);
    push(l,2);
    push(l,3);
    push(l,4);
    show(l);
    cout<<pop(l,e)<<endl;
    cout<<e<<"   <--------------"<<endl;
    show(l);

    cout<<pop(l,e)<<endl;
    cout<<e<<"   <--------------"<<endl;
    show(l);
    cout<<getTop(l,e)<<endl;
    cout<<e<<"   <--------------"<<endl;
    show(l);


    cout<<isEmpty(l)<<"  <========"<<endl;


}
bool initStack(LinkStack &l){
    l=(LinkNode *) malloc(sizeof(LinkNode));
    l->next=NULL;
    return true;
}
bool isEmpty(LinkStack l){
    return l->next==NULL;
}
bool push(LinkStack &l,int e){
    LinkNode *node= (LinkNode *)malloc(sizeof(LinkNode));
    node->data=e;
    node->next=l->next;
    l->next=node;
    return true;
}
bool pop(LinkStack &l,int &e){
    if(l->next==NULL)
        return false;
    e=l->next->data;
    LinkNode *n=l->next;
    l->next=l->next->next;
    free(n);
    return true;
}
bool getTop(LinkStack &l,int &e){
    if(l->next==NULL)
        return false;
    e=l->next->data;
    return true;
}
void show(LinkStack l){
    while (l->next!=NULL){
        l=l->next;
        cout<<l->data<<"    ";
    }
    cout<<endl;
}